﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Jw.Models;
using Jw.Core;
using Jw.Extend;
using System.Dynamic;
using Jw.Utils;
using System.Web.Http;


namespace Jw.Web.Areas.Weight.Controllers
{
    public class MaterialReportController : Controller
    {

        //
        // GET: /Weight/MaterialReport/
        /// <summary>
        /// 日报
        /// </summary>
        public List<dynamic> GetJinChangItems()
        {
            var result = new List<dynamic>();
            result.Add(new { value = "0", text = "已进厂" });
            result.Add(new { value = "1", text = "已出厂" });
            result.Add(new { value = "2", text = "异常" });
            return result;
        }

        public List<dynamic> GetWuZiItems()
        {
            var result = new List<dynamic>();
            result.Add(new { value = "原料", text = "原料" });
            result.Add(new { value = "设备", text = "设备" });
            result.Add(new { value = "建材", text = "建材" });
            result.Add(new { value = "废品", text = "废品" });
            result.Add(new { value = "电厂", text = "电厂" });
            result.Add(new { value = "成品", text = "成品" });
            result.Add(new { value = "备品", text = "备品" });
            return result;
        }

        public List<dynamic> GetJinChangFSItems()
        {
            var result = new List<dynamic>();
            result.Add(new { value = "0", text = "物资检斤" });
            result.Add(new { value = "1", text = "销售检斤" });
            result.Add(new { value = "2", text = "内转检斤" });
            return result;
        }

        public ActionResult DayReport()
        {
            var codeService = new sys_codeService();
            var model = new
            {
                urls = new
                {
                    query = "/api/Weight/MaterialReport/Get1/"
                },
                dataSource = new
                {
                    yearItems = codeService.GetYearItems(),
                    monthItems = codeService.GetMonthItems(),
                    jinchangFSItems = GetJinChangFSItems(),
                    jinchangItems = GetJinChangItems(),
                    wuziItems = GetWuZiItems()
                },
                form = new
                {
                    S_WPLX = "",
                    S_CPH = "",
                    D_RCSJ = "",
                    S_WZMC = "",
                    I_JCFS = "",
                    S_FHDWMC = ""
                }
            };
            return View(model);
        }
        /// <summary>
        /// 月报
        /// </summary>
        /// <returns></returns>
        public ActionResult MonthReport()
        {
            var codeService = new sys_codeService();
            var model = new
            {
                urls = new
                {
                    query = "/api/Weight/MaterialReport/GetMonthList/"
                    ///api/Weight/MaterialReport/MonthGet1/
                },
                dataSource = new
                {
                    yearItems = codeService.GetYearItems(),
                    monthItems = codeService.GetMonthItems(),
                    jinchangFSItems = GetJinChangFSItems(),
                    jinchangItems = GetJinChangItems(),
                    wuziItems = GetWuZiItems()
                },
                form = new
                {
                    S_WPLX = "",
                    S_CPH = "",
                    D_RCSJ = "",
                    S_WZMC = "",
                    I_JCFS = "",
                    S_FHDWMC = ""
                }
            };
            return View(model);
        }

        /// <summary>
        /// 年
        /// </summary>
        /// <returns></returns>
        public ActionResult YearReport()
        {
            var codeService = new sys_codeService();
            var model = new
            {
                urls = new
                {
                    //在控制器“MaterialReport”上找不到与名称“GET”匹配的操作
                    query = "/api/Weight/MaterialReport/YearGet/"
                },
                dataSource = new
                {
                    yearItems = codeService.GetYearItems(),
                    monthItems = codeService.GetMonthItems(),
                    jinchangFSItems = GetJinChangFSItems(),
                    jinchangItems = GetJinChangItems(),
                    wuziItems = GetWuZiItems()
                },
                form = new
                {
                    S_WPLX = "",
                    S_CPH = "",
                    D_RCSJ = "",
                    S_WZMC = "",
                    I_JCFS = "",
                    S_FHDWMC = ""
                }
            };
            return View(model);
        }
    }
    public class MaterialReportApiController : ApiController
    {

        public dynamic Get1(RequestWrapper query)
        {
            //第一种方法
            //var context = HttpContext.Current;
            //var request = context.Request;
            //string sCPH = request["S_CPH"];
            //第二种方法
            int page = ZConvert.To<int>(query["page"], 1);
            int rows = ZConvert.To<int>(query["rows"], 0);

            string S_WZMC = ZConvert.To<string>(query["S_WZMC"], "");
            string S_CPH = ZConvert.To<string>(query["S_CPH"], "");
            string S_FHDWMC = ZConvert.To<string>(query["S_FHDWMC"], "");

            string start, end;
            string D_CCSJ = ZConvert.To<string>(query["D_CCSJ"], "").Replace("到", "&");
            if (D_CCSJ.Split('&').Length > 1) //含有上下时间的情况
            {
                start = D_CCSJ.Split('&')[0];
                end = D_CCSJ.Split('&')[1];
                if (end == null || end == "")
                    end = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
            }
            else if (D_CCSJ.Split('&').Length == 1 && D_CCSJ != "")
            {
                start = D_CCSJ.Split('&')[0];
                end = Convert.ToDateTime(D_CCSJ).AddDays(1).ToString("yyyy-MM-dd");
            }
            else
            {
                start = DateTime.Now.ToString("yyyy-MM-dd");
                end = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
            }

            string I_JCFS = ZConvert.To<string>(query["I_JCFS"], "");
            string S_WPLX = ZConvert.To<string>(query["S_WPLX"], "");
            //

            var db = Db.Context("Weight");
            List<tempMateral> temp = new MaterialReport().GetList(db.Data.ConnectionString, S_WZMC, S_WPLX, S_CPH, S_FHDWMC, I_JCFS, start, end);
            dynamic result = new ExpandoObject();
            if (rows == 0)
            {
                //导出excel用到
                result.rows = temp;
            }
            else
            {
                //正常
                page = 1; rows = 10000;
                result.rows = temp.Skip((page - 1) * rows).Take(rows);
            }
            result.total = temp.Count;
            return result;
        }


        public dynamic GetMonthList(RequestWrapper query)
        {
            int page = ZConvert.To<int>(query["page"], 1);
            int rows = ZConvert.To<int>(query["rows"], 0);

            string S_WZMC = ZConvert.To<string>(query["S_WZMC"], "");
            string S_CPH = ZConvert.To<string>(query["S_CPH"], "");
            string S_FHDWMC = ZConvert.To<string>(query["S_FHDWMC"], "");
            string I_JCFS = ZConvert.To<string>(query["I_JCFS"], "");
            string S_WPLX = ZConvert.To<string>(query["S_WPLX"], "");

            string start, end;
            string D_CCSJ = ZConvert.To<string>(query["D_CCSJ"], "");
            if (D_CCSJ == "") //时间未选 不进行搜索
                return null;
            else
            {
                start = DateTime.Now.Year + D_CCSJ.Trim().Length == 1 ? ("-0" + D_CCSJ) : ("-" + D_CCSJ) + "-00";
                end = DateTime.Now.AddYears(1).Year + D_CCSJ.Trim().Length == 1 ? ("-0" + D_CCSJ) : ("-" + D_CCSJ) + "-00";
            }

            var db = Db.Context("Weight");
            List<tempMateral> temp = new MaterialReport().GetList(db.Data.ConnectionString, S_WZMC, S_WPLX, S_CPH, S_FHDWMC, I_JCFS, start, end);
            dynamic result = new ExpandoObject();
            if (rows == 0)
            {
                //导出excel用到
                result.rows = temp;
            }
            else
            {
                //正常
                page = 1; rows = 10000;
                result.rows = temp.Skip((page - 1) * rows).Take(rows);
            }
            result.total = temp.Count;
            return result;
        }

        public dynamic YearGet(RequestWrapper query)
        {
            int page = ZConvert.To<int>(query["page"], 1);
            int rows = ZConvert.To<int>(query["rows"], 0);

            string S_WZMC = ZConvert.To<string>(query["S_WZMC"], "");
            string S_CPH = ZConvert.To<string>(query["S_CPH"], "");
            string S_FHDWMC = ZConvert.To<string>(query["S_FHDWMC"], "");
            string I_JCFS = ZConvert.To<string>(query["I_JCFS"], "");
            string S_WPLX = ZConvert.To<string>(query["S_WPLX"], "");

            string start, end;
            string D_CCSJ = ZConvert.To<string>(query["D_CCSJ"], "").Replace("到", "&");
            if (D_CCSJ == "") //时间未选
            {
                start = DateTime.Now.ToString("yyyy-MM-dd");
                end = DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd");
            }
            else
            {
                start = D_CCSJ.Split('&')[0];
                end = Convert.ToDateTime(D_CCSJ).AddDays(1).ToString("yyyy-MM-dd");
            }

            var db = Db.Context("Weight");
            List<tempMateral> temp = new MaterialReport().GetList(db.Data.ConnectionString, S_WZMC, S_WPLX, S_CPH, S_FHDWMC, I_JCFS, start, end);
            dynamic result = new ExpandoObject();
            if (rows == 0)
            {
                //导出excel用到
                result.rows = temp;
            }
            else
            {
                //正常
                page = 1; rows = 10000;
                result.rows = temp.Skip((page - 1) * rows).Take(rows);
            }
            result.total = temp.Count;
            return result;
        }

    }
}
